package com.developer.platform.controller.system;

import com.developer.platform.application.command.RoleCmdManager;
import com.developer.platform.application.command.cmd.RoleAdd;
import com.developer.platform.application.command.cmd.RoleUpdate;
import com.developer.platform.application.query.RoleQueryManager;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.RoleDto;
import com.developer.platform.application.query.dto.RoleListDto;
import com.developer.platform.application.query.dto.UserRoleDto;
import com.developer.platform.controller.BaseController;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色控制器
 *
 * @author ：qiaoliang
 * @date ：2020-11-03
 */
@Api(tags = "角色管理")
@Validated
@RequestMapping("/system/role")
@RestController
public class RoleController extends BaseController {

    private final RoleQueryManager roleQueryManager;
    private final RoleCmdManager roleCmdManager;


    public RoleController(RoleQueryManager roleQueryManager,
                          RoleCmdManager roleCmdManager) {
        this.roleQueryManager = roleQueryManager;
        this.roleCmdManager = roleCmdManager;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "名称", name = "name", required = false),
            @ApiImplicitParam(value = "状态", name = "status", required = false),
            @ApiImplicitParam(value = "页大小", name = "pageSize", required = true),
            @ApiImplicitParam(value = "当前页", name = "pageNumber", required = true),
    })
    @Log(businessType = "角色管理",discription = "查询角色列表")
    @ApiOperation(value = "查询角色列表")
    @GetMapping
    public ResponseEntity<ResponseModel<RoleListDto>> list(@RequestParam(value = "name" , required = false) String name,
                                                           @RequestParam(value = "status" , required = false) StatusEnum status,
                                                           Pageable pageable){
        return ok(roleQueryManager.list(name, status, pageable));
    }

    @Log(businessType = "角色管理",discription = "查询角色及用户包含的角色")
    @ApiOperation(value = "查询角色及用户包含的角色")
    @GetMapping("/user/{userId}")
    public ResponseEntity<UserRoleDto> list(@PathVariable("userId") long userId){
        return ResponseEntity.ok(roleQueryManager.userRoleByUserId(userId));
    }


    @Log(businessType = "角色管理",discription = "查询正常状态角色集合")
    @ApiOperation(value = "查询正常状态角色集合")
    @GetMapping("/normal")
    public ResponseEntity<List<RoleDto>> list(){
        return ResponseEntity.ok(roleQueryManager.list(StatusEnum.Y));
    }


    @Log(businessType = "角色管理",discription = "新增角色信息")
    @ApiOperation(value = "新增角色信息")
    @PostMapping
    public ResponseEntity<?> save(@RequestBody RoleAdd roleAdd){
        roleCmdManager.save(roleAdd);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "角色管理",discription = "修改角色信息")
    @ApiOperation(value = "修改角色信息")
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") long id,
                                    @RequestBody RoleUpdate roleUpdate){
        roleCmdManager.update(id, roleUpdate);
        return ResponseEntity.status(HttpStatus.RESET_CONTENT).build();
    }

    @Log(businessType = "角色管理",discription = "删除角色信息")
    @ApiOperation(value = "删除角色信息")
    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") long id){
        roleCmdManager.delete(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
